/*
 *使用示例：
 *function selectDept(){
	   if(!deptWin){
		   deptWin= new deptDialog();
		   deptWin.on('onSelectItem', function(deptCode, name) {
				form.getForm().findField('pDeptCode').setValue(deptCode);
				form.getForm().findField('pDeptName').setValue(name);
				}, this);
			}
	   deptWin.show();
   }
 * */
Ext.define('deptDialog', {
			extend : "Ext.window.Window",
			hiddenDeptCode: '',
			hiddenName : '',
				title : "部门选择",
			initComponent : function() {
				var store = Ext.create('Ext.data.TreeStore', {
							proxy : {
								type : 'ajax',
								url : '../departmentController/getTreeJson.action'
							},
							sorters : [{
										property : 'leaf',
										direction : 'ASC'
									}, {
										property : 'text',
										direction : 'ASC'
									}],
							root : {
								id : 'root-0',
								text : '所有部门',
								checked : false,
								expandable : true,
								expanded : true
							}
						});
				this.treePanel = new Ext.tree.Panel({
							title : "双击选择部门",
							region : "west",
							width : 180,
							id : "deptTree",
							height : 300,
							split : true,
							collapsible : true,
							autoScroll : true,
							tbar : new Ext.Toolbar({
										items : [{
													xtype : "button",
													iconCls : "btn-refresh",
													text : "刷新",
													handler : function() {
														Ext.getCmp("deptTree").store.reload();
													}
												}, "-", {
													xtype : "button",
													text : "展开",
													iconCls : "btn-expand",
													handler : function() {
														Ext.getCmp("deptTree").expandAll();
													}
												}, "-", {
													xtype : "button",
													text : "收起",
													iconCls : "btn-collapse",
													handler : function() {
														Ext.getCmp("deptTree").collapseAll();
													}
												}]
									}),
							store : store,
							rootVisible : false,
							listeners : {
								"itemclick" : function(view, rcd, item, idx,
										event, eOpts) {
								
									Ext.getCmp("depSelectWindow").hiddenDeptCode=rcd.get('id');
									Ext.getCmp("depSelectWindow").hiddenName = rcd.get('text');
								},
								"itemdblclick" : function(view, rcd, item, idx,
										event, eOpts) {
								    Ext.getCmp("depSelectWindow").hiddenDeptCode=rcd.get('id');
									Ext.getCmp("depSelectWindow").hiddenName = rcd.get('text');
									Ext.getCmp("depSelectWindow").selectItem();
								}
							}
						});

				this.id = "depSelectWindow";
				this.items = [this.treePanel];
				this.modal = true; // 是否为模态窗口
				this.resizable = false; // 是否可以改变窗口大小
				this.buttons = [{
							iconCls : "btn-ok",
							text : "确定",
							scope : this,
							handler : this.selectItem
						}, {
							text : "取消",
							iconCls : "btn-cancel",
							handler : this.onWinHide,
							scope : this
						}];
							this.addEvents({// 加入自定义事件
					"onSelectItem" : true
				});
				this.callParent();
			
			},
			selectItem : function() {
				if (this.hiddenId != '') {
					this.fireEvent('onSelectItem', this.hiddenDeptCode,
							this.hiddenName);// 激发自定义事件
					this.hide();
				} else {
					Ext.Msg.alert("提示", "请选择部门！");
				}
			},
			onWinHide : function() {
				this.hide();
			}
		});
